package dao.Impl;

import entity.SalaryStandard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class SalaryChange {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String SQL_INSERT_SALARY_STANDARD =
            "INSERT INTO change_standard (salary_standard_id,standard_name, basic_salary, salary_standard_bonus, salary_standard_allowance, " +
                    "salary_standard_insurance, total_amount, salary_standard_status, create_user_id, update_user_id) " +
                    "VALUES (?,?, ?, ?, ?, ?, ?, ?, ?, ?)";

    private static final String SQL_FIND_BY_ID =
            "SELECT * FROM change_standard WHERE salary_standard_id = ?";

    private static final String SQL_FIND_ALL =
            "SELECT * FROM change_standard";

    private static final String SQL_UPDATE_SALARY_STANDARD =
            "UPDATE change_standard SET standard_name = ?, basic_salary = ?, salary_standard_bonus = ?, " +
                    "salary_standard_allowance = ?, salary_standard_insurance = ?, total_amount = ?, " +
                    "salary_standard_status = ?, update_user_id = ? WHERE salary_standard_id = ?";

    private static final String SQL_DELETE_BY_ID =
            "DELETE FROM change_standard WHERE salary_standard_id = ?";


    public void create(SalaryStandard salaryStandard) {
        jdbcTemplate.update(SQL_INSERT_SALARY_STANDARD,
                salaryStandard.getSalaryStandardId(),
                salaryStandard.getStandardName(),
                salaryStandard.getBasicSalary(),
                salaryStandard.getSalaryStandardBonus(),
                salaryStandard.getSalaryStandardAllowance(),
                salaryStandard.getSalaryStandardInsurance(),
                salaryStandard.getTotalAmount(),
                salaryStandard.getSalaryStandardStatus(),
                salaryStandard.getCreateUserId(),
                salaryStandard.getUpdateUserId());
    }

    public SalaryStandard findById(Long salaryStandardId) {
        return jdbcTemplate.queryForObject(SQL_FIND_BY_ID, new Object[]{salaryStandardId},
                BeanPropertyRowMapper.newInstance(SalaryStandard.class));
    }

    public List<SalaryStandard> findAll() {
        return jdbcTemplate.query(SQL_FIND_ALL,
                BeanPropertyRowMapper.newInstance(SalaryStandard.class));
    }

    public void update(SalaryStandard salaryStandard) {
        jdbcTemplate.update(SQL_UPDATE_SALARY_STANDARD,
                salaryStandard.getStandardName(),
                salaryStandard.getBasicSalary(),
                salaryStandard.getSalaryStandardBonus(),
                salaryStandard.getSalaryStandardAllowance(),
                salaryStandard.getSalaryStandardInsurance(),
                salaryStandard.getTotalAmount(),
                salaryStandard.getSalaryStandardStatus(),
                salaryStandard.getUpdateUserId(),
                salaryStandard.getSalaryStandardId());
    }

    public void deleteById(Long salaryStandardId) {
        jdbcTemplate.update(SQL_DELETE_BY_ID, salaryStandardId);
    }
}
